<?php

namespace backend\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use backend\models\Customer;

/**
 * Customersearch represents the model behind the search form about `backend\models\Customer`.
 */
class Customersearch extends Customer
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id', 'type', 'advantage', 'province', 'project_province', 'project_type', 'company_type', 'company_level', 'principal', 'review_level', 'company_province', 'company_city', 'is_need_clear'], 'integer'],
            [['url', 'company', 'contact', 'image', 'history', 'next_date', 'file', 'investment', 'volume', 'remark', 'type_desc', 'package_type', 'package_transfer', 'review_level', 'first_mark_A', 'owner', 'tag', 'need', 'market_area'], 'safe'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = Customer::find();

        // add conditions that should always apply here

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
            'sort' => [
                'defaultOrder' => [
                    'id' => SORT_DESC
                ]]
        ]);

        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        // grid filtering conditions
        $query->andFilterWhere([
            'id' => $this->id,
            'next_date' => $this->next_date,
            'project_province' => $this->project_province,
            'project_type' => $this->project_type,
            'company_type' => $this->company_type,
            'company_level' => $this->company_level,
//            'type_desc' => $this->type_desc,
            'principal' => $this->principal,
            'package_type' => $this->package_type,
            'package_transfer' => $this->package_transfer,
            'company_province' => $this->company_province,
            'company_city' => $this->company_city,
            'review_level' => $this->review_level,
            'first_mark_A' => $this->first_mark_A,
            'owner' => $this->owner,
            'is_need_clear' => $this->is_need_clear,

        ]);

        $query->andFilterWhere(['like', 'company', $this->company])
            ->andFilterWhere(['like', 'contact', $this->contact])
            ->andFilterWhere(['like', 'history', $this->history])
            ->andFilterWhere(['like', 'investment', $this->investment])
            ->andFilterWhere(['like', 'volume', $this->volume])
            ->andFilterWhere(['like', 'need', $this->need])
            ->andFilterWhere(['like', 'market_area', $this->market_area])
            ->andFilterWhere(['like', 'remark', $this->remark]);

        if ($this->type_desc) {
            $query->andWhere("find_in_set($this->type_desc,`type_desc`)");
        }
        if ($this->advantage) {
            $query->andWhere("find_in_set($this->advantage,`advantage`)");
        }
        if ($this->province) {
            $query->andWhere("find_in_set($this->province,`province`)");
        }

        if ($this->tag) {
//            $this->tag = implode(',', $this->tag);
            $query->andWhere("find_in_set('".$this->tag."',`tag`)");
        }

        return $dataProvider;
    }
}
